#pragma once

#include "Task.h"
#include "PowerService.h"

#define SLEEP_TASK "sleep_task"

class SleepTask : public Task
{
private:
	uint8_t sleepTimes = 0;
	bool sleep = false;
public:
	void execute() {

		if (digitalRead(GPIO_NUM_39) == 0) {
			sleepTimes++;
		} else {
			trySleep();
			reset();
		}

		if (sleepTimes > 7) {
			sleep = true;
			return;
		}

		sleep = false;	
	}

	void trySleep() {
		if (sleep) {
			powerService.sleep();
		}
	}

	void reset() {
		sleepTimes = 0;
		sleep = false;
	}
public:
	SleepTask() : Task(SLEEP_TASK, false) {};
	~SleepTask() {};
};

